// https://www.luogu.com.cn/problem/P2299
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
const int maxn = 3 + 1e4;
const int inf = 0x3f3f3f3f;
using Node = pair<int, int>;

int n, m;
vector<Node> adj[maxn];  // <v, w>

// Dijkstra
bool visit[maxn];
int dis[maxn];
priority_queue<Node, vector<Node>, greater<Node>> qe;  // <dis, node>
void Dijkstra(int start = 1) {
    memset(dis, inf, sizeof(dis));
    dis[start] = 0;
    qe.push({0, start});
    while (!qe.empty()) {
        int u = qe.top().second;
        qe.pop();
        if (visit[u]) continue;
        visit[u] = 1;
        // update
        for (auto &node : adj[u]) {
            int v = node.first, w = node.second;
            if (!visit[v] && dis[v] > dis[u] + w) {
                dis[v] = dis[u] + w;
                qe.push({dis[v], v});
            }
        }
    }
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; ++i) {
        int l, r, w;
        scanf("%d%d%d", &l, &r, &w);
        adj[l].push_back({r, w});
        adj[r].push_back({l, w});
    }
    Dijkstra(1);
    printf("%d", dis[n]);
}